package com.lbj.googleplay.googleplay100.ui.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.lbj.googleplay.googleplay100.manager.ThreadManager;
import com.lbj.googleplay.googleplay100.ui.holder.BaseHolder;
import com.lbj.googleplay.googleplay100.ui.holder.MoreHolder;
import com.lbj.googleplay.googleplay100.utils.UIUtils;

import java.util.ArrayList;

public abstract class MyBaseAdapter<T> extends BaseAdapter {

    private ArrayList<T> list;

    private  static  final  int TYPE_NORMAL = 1;//普通类型
    private  static  final  int TYPE_MORE = 0;//加载更多类型

    private boolean isLoadMore = false;//标记是否正在加载更多

    public MyBaseAdapter(ArrayList<T> list){
        this.list = list;
    }
    
    @Override
    public int getCount() {
        return list.size() +1;
    }

    @Override
    public T getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseHolder holder;
        if (convertView==null){
            //判断该显示哪种类型的item
            if (getItemViewType(position)==TYPE_MORE){
                //加载更多的类型
            holder = new MoreHolder(hasMore());
            }else {
                //加载普通的类型
                holder = getHolder(position);
            }
        }else {
            holder = (BaseHolder) convertView.getTag();
        }
            if (getItemViewType(position)!=TYPE_MORE){

                holder.setData(getItem(position));

            }else {
            //加载更多布局
            //一旦加载更多布局展示出来,就开始加载更多
                MoreHolder moreHolder = (MoreHolder) holder;
                if (moreHolder.getData()==MoreHolder.STATE_MORE_MORE){
                    loadMore(moreHolder);
                }

            }

        return holder.getmRootView();
    }
    //返回布局类型个数
    @Override
    public int getViewTypeCount() {

        return 2;//返回2种类,普通和加载更多
    }
    //返回当前位置item应该展示的那种类型
    @Override
    public int getItemViewType(int position) {

        if (position==getCount()-1){
            //说明是最后一个item,应展示加载更多布局
            return TYPE_MORE;
        }else {
            return getInnerType(position);
        }
    }
    //子类可以重写此方法来更改返回的类型
    public int getInnerType(int position){
        return TYPE_NORMAL;
    }

    public  boolean hasMore(){
        return true;//默认都是有更多数据的
    }

    //加载更多数据

    public void loadMore(final MoreHolder moreHolder){
        if (!isLoadMore) {
            isLoadMore = true;
//            new Thread() {
//                @Override
//                public void run() {
//                    //获取数据
//                    final ArrayList<T> moreData = onLoadMore();
//                    //去主线程刷新ui
//                    UIUtils.runOnUIThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            if (moreData != null) {//获取数据成功
//                                if (moreData.size() < 20) {//没有更多数据了
//                                    moreHolder.setData(MoreHolder.STATE_MORE_NONE);
//                                    Toast.makeText(UIUtils.getContext(), "没有更多数据了", Toast.LENGTH_SHORT).show();
//                                } else {//还有更多数据
//                                    moreHolder.setData(MoreHolder.STATE_MORE_MORE);
//                                }
//                                //将数据添加到集合中
//                                list.addAll(moreData);
//                                //刷新ui
//                                MyBaseAdapter.this.notifyDataSetChanged();
//                            } else {
//                                //获取数据失败
//                                moreHolder.setData(MoreHolder.STATE_MORE_ERROR);
//                            }
//                            isLoadMore = false;
//                        }
//                    });
//
                }
//            }.start();

            ThreadManager.getThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    //获取数据
                    final ArrayList<T> moreData = onLoadMore();
                    //去主线程刷新ui
                    UIUtils.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            if (moreData != null) {//获取数据成功
                                if (moreData.size() < 20) {//没有更多数据了
                                    moreHolder.setData(MoreHolder.STATE_MORE_NONE);
                                    Toast.makeText(UIUtils.getContext(), "没有更多数据了", Toast.LENGTH_SHORT).show();
                                } else {//还有更多数据
                                    moreHolder.setData(MoreHolder.STATE_MORE_MORE);
                                }
                                //将数据添加到集合中
                                list.addAll(moreData);
                                //刷新ui
                                MyBaseAdapter.this.notifyDataSetChanged();
                            } else {
                                //获取数据失败
                                moreHolder.setData(MoreHolder.STATE_MORE_ERROR);
                            }
                            isLoadMore = false;
                        }
                    });

                }
            });
        }

    //返回当前集合的大小
    public int getListSize(){
        return list.size();
    }
    //返回当前页面的holder对象,必须子类实现
    public abstract BaseHolder<T> getHolder(int position);
    //加载更多数据,子类实现
    public abstract ArrayList<T> onLoadMore();
}
